package krt.wid.gslzs.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;


import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Response;

import java.util.List;

import krt.wid.gslzs.adpater.GridViewAdapter;
import krt.wid.gslzs.bean.Choose_BM;
import krt.wid.gslzs.R;
import krt.wid.http.MHttp;
import krt.wid.http.MResponseListener;
import krt.wid.http.Result;
import krt.wid.util.MToast;


/**
 * Created by Administrator on 2017-08-07.
 */

public class CommomDialog extends Dialog implements View.OnClickListener {

    private Context mContext;
    private String content;
    private OnCloseListener listener;
    private EditText search;

    Button choose_subit;


    private List<Choose_BM> dataList;
    private GridViewAdapter adapter;
    GridView gridView;


    public CommomDialog(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }


    public CommomDialog(Context context, int themeResId, String content) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
    }


    public CommomDialog(Context context, int themeResId, String content, List<Choose_BM> list, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
        this.dataList = list;

    }

    protected CommomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
        setCanceledOnTouchOutside(true);
        initView();
        search = findViewById(R.id.searchinfo);
        choose_subit = findViewById(R.id.choose_subit);
        findViewById(R.id.searchbtn).setOnClickListener(this);
        choose_subit.setOnClickListener(this);

    }


    private void initView() {
        gridView = findViewById(R.id.gridview);
        adapter = new GridViewAdapter(getContext(), dataList);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (dataList.get(i).isSelect()) {
                    MToast.showToast(mContext, dataList.get(i).getDepTypeName());
                }
            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.choose_subit:
                if (listener != null) {
                    listener.onClick(this, adapter.getList(), true);
                }
                break;
            case R.id.searchbtn:
                if (search.getText().toString().trim().isEmpty()) {
                    MToast.showToast(mContext, "搜索内容为空!");
                    return;
                }
                MHttp.<Result<List<Choose_BM>>>create((Activity) mContext)
                        .setMethod(RequestMethod.GET)
                        .setUrl("http://111.75.240.74:13381/non_public_economy/api/depList")
                        .addParams("accessToken", new SpUtil(mContext).getAccessToken())
                        .addParams("depName", search.getText().toString())
                        .execute(new MResponseListener<Result<List<Choose_BM>>>() {
                            @Override
                            public void onSuccess(int what, Result<List<Choose_BM>> listResult) {
                                if (listResult.isSuccess()) {
                                    adapter.setList(listResult.getData());
                                } else {
                                    MToast.showToast(mContext, listResult.getMsg());
                                }
                            }

                            @Override
                            public void onFailed(int what, Response<Result<List<Choose_BM>>> response) {

                            }
                        });

                break;
            default:
        }
    }

    public interface OnCloseListener {
        void onClick(Dialog dialog, List<Choose_BM> list, boolean confirm);
    }
}

